package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SysDictTypeMapper;
import com.itheima.pojo.Link;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    /**
     * 添加字典类型
     * @param sysDictType
     */

    @Override
    public void add(SysDictType sysDictType) {
        IdWorker idWorker=new IdWorker();
        long id = idWorker.nextId();
        sysDictType.setUid(String.valueOf(id));
        sysDictType.setCreateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictType.setUpdateByUid("1f01cd1d2f474743b241d74008b12333");
        sysDictType.setCreateTime(new Date());
        sysDictType.setUpdateTime(new Date());
        sysDictType.setStatus(1);
        sysDictTypeMapper.insert(sysDictType);

    }

    /**
     * 批量删除字典类型
     * @param sysDictTypeList
     */
    @Override
    public void deleteBatch(List<SysDictType> sysDictTypeList) {
        if (sysDictTypeList.size()>0&&sysDictTypeList!=null){
            for (SysDictType sysDictType : sysDictTypeList) {

                sysDictTypeMapper.deleteByPrimaryKey(sysDictType.getUid());
            }
        }


    }
    /**
     * 编辑字典类型
     * @param sysDictType
     */
    @Override
    public void edit(SysDictType sysDictType) {
        SysDictType sysDictType1 = sysDictTypeMapper.selectByPrimaryKey(sysDictType.getUid());
        sysDictType.setCreateByUid(sysDictType1.getCreateByUid());
        sysDictType.setUpdateByUid(sysDictType1.getUpdateByUid());
        sysDictType.setCreateTime(sysDictType1.getCreateTime());
        sysDictType.setUpdateTime(new Date());

        sysDictTypeMapper.deleteByPrimaryKey(sysDictType1.getUid());
        sysDictTypeMapper.insert(sysDictType);

    }
    /**
     * 多条件分页查询
     * @param sysDictType
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<SysDictType> getList(SysDictType sysDictType, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(sysDictType);
        return (Page<SysDictType>)sysDictTypeMapper.selectByExample(example);
    }
    /**
     *
     * @param sysDictType
     * @return
     */
    private Example createExample(SysDictType sysDictType){
        Example example=new Example(SysDictType.class);
        Example.Criteria criteria = example.createCriteria();
        if(sysDictType!=null){
            // uid
            if(sysDictType.getUid()!=null && !"".equals(sysDictType.getUid())){
                criteria.andEqualTo("uid",sysDictType.getUid());
            }
            // oid
            if(sysDictType.getOid()!=null && !"".equals(sysDictType.getOid())){
                criteria.andEqualTo("oid",sysDictType.getOid());
            }
            // 字典名称
            if(sysDictType.getDictName()!=null && !"".equals(sysDictType.getDictName())){
                criteria.andLike("dictName","%"+sysDictType.getDictName()+"%");
            }
            // 字典类型
            if(sysDictType.getDictType()!=null && !"".equals(sysDictType.getDictType())){
                criteria.andEqualTo("dictType",sysDictType.getDictType());
            }
            // 备注
            if(sysDictType.getRemark()!=null && !"".equals(sysDictType.getRemark())){
                criteria.andLike("remark","%"+sysDictType.getRemark()+"%");
            }
            //状态
            if(sysDictType.getStatus()!=null && !"".equals(sysDictType.getStatus())){
                criteria.andEqualTo("status",sysDictType.getStatus());
            }
            //是否发布(1:是，0:否)
            if(sysDictType.getIsPublish()!=null && !"".equals(sysDictType.getIsPublish())){
                criteria.andEqualTo("isPublish",sysDictType.getIsPublish());
            }

        }
        return example;
    }
}
